package com.jali.crud.controller;

import com.jali.crud.bean.Emp;
import com.jali.crud.bean.JaTaskItem;
import com.jali.crud.bean.Msg;
import com.jali.crud.service.EmpService;
import com.jali.crud.service.JaTaskItemService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author jali
 * @create 2018-12-04 17:26
 */
@Controller
public class EmpController {

    @Autowired
    EmpService empService;

    @Autowired
    JaTaskItemService jaTaskItemService;

    @RequestMapping(value = "/emp/{ids}",method = RequestMethod.DELETE)
    @ResponseBody
    public Msg deleteById(@PathVariable("ids") String ids){

        if(ids.contains("-")){
            String[] idStrs = ids.split("-");
            List<Integer> idsList = new ArrayList<>();
            for(String idStr : idStrs){
                idsList.add(Integer.parseInt(idStr));
            }
            empService.deleteBatch(idsList);
        }else{
            Integer id = Integer.parseInt(ids);
            empService.deleteEmp(id);
        }
        return Msg.success();
    }

    /**
     * 如果直接发送ajax=put形式的请求
     * 封装的数据
     * Empp[id=1,empName=null,gender=null,email=null,deptid=null]
     * @param emp
     * @return
     */
    @RequestMapping(value = "/emp/{id}",method = RequestMethod.PUT)
    @ResponseBody
    public Msg updateEmp(Emp emp){
        System.out.println(emp.toString());
        empService.updatEmp(emp);
        return Msg.success();
    }

    @RequestMapping(value = "/emp/{id}",method = RequestMethod.GET)
    @ResponseBody
    public Msg getEmp(@PathVariable Integer id){
        Emp emp = empService.getEmp(id);
        return Msg.success().add("emp",emp);
    }

    @RequestMapping(value = "/emp",method = RequestMethod.POST)
    @ResponseBody
    public Msg saveEmp(Emp emp){
        empService.save(emp);
        return Msg.success();
    }

    @RequestMapping("/checkUser")
    @ResponseBody
    public Msg checkUser(@RequestParam(value = "empName") String empName){
        //先判断用户名是否是合法的表达式
        String regx = "(^[a-zA-Z0-9_-]{3,16}$)|(^[\\u2E80-\\u9FFF]{2,5})";
        if(!empName.matches(regx)){
            return Msg.fail().add("va_msg","用户名必须是2-5位中文或者3-16位英文和数字的组合");
        }
        if(empService.checkUser(empName)){
            return Msg.success();
        }else{
            return Msg.fail().add("va_msg","用户名不可用");
        }

    }

    /**
     * 导入json
     * @param pn
     * @return
     */
    @RequestMapping("/emps")
    @ResponseBody
    public Msg getEmpsWithJson(@RequestParam(value = "pn",defaultValue = "1") Integer pn){
        //在查询之前只需要调用，传入页码，以及每页的大小
        PageHelper.startPage(pn,5);
        List<Emp> emps =  empService.getEmps();
        //使用pageInfo包装查询后的结果，只需要将pageInfo交给页面就行了
        //封装了详细的分页详细，包括有我们查询出来的数据，传入连续显示的页数
        PageInfo pageInfo = new PageInfo(emps,5);
        return Msg.success().add("pageInfo",pageInfo);
    }

    //@RequestMapping("/emps")
    public String getEmps(@RequestParam(value = "pn",defaultValue = "1") Integer pn, Model model){
        List<JaTaskItem> list = jaTaskItemService.selectTaskItems();
        for(JaTaskItem jaTaskItem : list){
            System.out.println(jaTaskItem.getTitle());
        }

        //在查询之前只需要调用，传入页码，以及每页的大小
        PageHelper.startPage(pn,5);
        List<Emp> emps =  empService.getEmps();
        //使用pageInfo包装查询后的结果，只需要将pageInfo交给页面就行了
        //封装了详细的分页详细，包括有我们查询出来的数据，传入连续显示的页数
        PageInfo pageInfo = new PageInfo(emps,5);
        model.addAttribute("pageInfo",pageInfo);
        return "list";
    }

}
